package com.test.java1;

import java.util.Scanner;

public class T1012_HuaWei {
    /**
     * (1-2)*3+4
     * 10  : 进制
     * 输出 1
     * @param args
     */
    public static void main(String[] args) {
        String s = "";
        Scanner in = new Scanner(System.in);
        s = in.nextLine();
        int t = Integer.parseInt(in.nextLine());
        int ans = calculate(s, t);
        System.out.println(Integer.toString(ans, t));
    }
    static char[] Mapping = new char[]{'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','-'};
    static String converse(int digital,int weight){
        if (weight == 10){
            return String.valueOf(digital);
        }
        int flag = 0;
        if (digital < 0){
            flag = 1;
            digital = - digital;
        }
        String str = "";
        int div=0;
        int remain=0;
        while (digital != 0){
            div = digital / weight;
            remain = digital - div * weight;
            digital = div;
            str = Mapping[remain] + str;
        }
        if (flag==1){
            str = Mapping[16] + str;
        }
        return str;
    }
    static int calculate(String s, int jin){
        int n = s.length();
        int num = 0, curRes = 0, res = 0;
        char op = '+';
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            if ((c>='0' && c <= '9') || (c>='a' && c<='f')){
                int t = 0;
                if (c>='0' && c <= '9'){
                    t = c - '0';
                }else {
                    t = c - 'a' + 10;
                }
                num = num * jin + t;
            }else if (c == '('){
                int j = i, cnt = 0;
                for (; i < n; i++) {
                    if (s.charAt(i) == '(')
                        cnt++;
                    if (s.charAt(i) == ')')
                        cnt--;
                    if (cnt == 0)
                        break;
                }
                num = calculate(s.substring(j+1, i),jin);
            }
            if (c == '+' || c == '-' || c == '*' || c == '/' || i == n-1){
                switch (op){
                    case '+': curRes += num; break;
                    case '-': curRes -= num; break;
                    case '*': curRes *= num; break;
                    case '/': curRes /= num; break;
                }
                if (c == '+' || c == '-' || i == n-1){
                    res += curRes;
                    curRes = 0;
                }
                op = c;
                num = 0;
            }
        }
        return res;
    }
}
